baseUrl = "http://localhost:8088";

// 添加/更新数据
function setCookie(key, value, days = 1) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = key + "=" + encodeURIComponent(value) + expires + "; path=/";
}

// 删除数据
function deleteCookie(key) {
    setCookie(key, "", -1);
}

// 检查数据是否存在
function hasCookie(key) {
    var cookies = document.cookie.split(";");
    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i].trim();
        if (cookie.startsWith(key + "=")) {
            return true;
        }
    }
    return false;
}

function getCookieValue(key) {
    var cookies = document.cookie.split(";");
    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i].trim();
        if (cookie.startsWith(key + "=")) {
            return decodeURIComponent(cookie.substring(key.length + 1));
        }
    }
    return undefined;
}

function showPopup(message) {
    const popup = document.getElementById("popup");
    const popupText = document.getElementById("popup-text");

    // 设置弹窗内容
    popupText.textContent = message;

    // 动态调整弹窗宽度
    const contentWidth = popupText.offsetWidth + 20; // 添加一些额外的 padding
    popup.style.width = Math.max(contentWidth, 200) + "px";

    // 显示弹窗
    popup.classList.remove("fade-out");
    popup.style['display'] = '';

    // 定时淡出
    setTimeout(() => {
        popup.classList.add("fade-out");
        popup.style['display'] = 'none';
    }, 3000);
}



function verifyTimeFormat(timeString) {
    const regex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
    return regex.test(timeString);
}

function getCurrentTime() {
    const currentDate = new Date();

    const year = currentDate.getFullYear();
    const month = String(currentDate.getMonth() + 1).padStart(2, '0');
    const day = String(currentDate.getDate()).padStart(2, '0');
    const hours = String(currentDate.getHours()).padStart(2, '0');
    const minutes = String(currentDate.getMinutes()).padStart(2, '0');
    const seconds = String(currentDate.getSeconds()).padStart(2, '0');

    const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

    return formattedTime;
}

function getTimeWithOffset(offsetInSeconds) {
    const currentDate = new Date();

    const offsetMilliseconds = offsetInSeconds * 1000; // 将秒转换为毫秒
    const targetDate = new Date(currentDate.getTime() + offsetMilliseconds);

    const year = targetDate.getFullYear();
    const month = String(targetDate.getMonth() + 1).padStart(2, '0');
    const day = String(targetDate.getDate()).padStart(2, '0');
    const hours = String(targetDate.getHours()).padStart(2, '0');
    const minutes = String(targetDate.getMinutes()).padStart(2, '0');
    const seconds = String(targetDate.getSeconds()).padStart(2, '0');

    const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

    return formattedTime;
}

function isValidTime(timeString) {
    const date = new Date(timeString);
    return date instanceof Date && !isNaN(date);
}

function convertDataToISOString(dateTimeString) {
    const date = new Date(dateTimeString);
    const isoString = date.toISOString();
    return isoString;
  }
  